<?php
class MysqlDb{
	private $_host;
	private $_user;
	private $_pass;
	private $_char;
	private $_db;
	private $_link;
      private static $instance;
      private function __construct($config){
            $this->_init($config);
            $this->_connect();
            $this->_charset();
            $this->_dbselect();
      }
      private function __clone(){
      }
      public static function getInstance(){
            if (!isset(static::$instance)) {
                 static::$instance = new MysqlDb($config = array());
            }
            return static::$instance;
      }
	private function _init($config)
	{
		$this->_host=isset($config['host'])?$config['host']:'localhost';
		$this->_char=isset($config['char'])?$config['char']:'utf8';
		$this->_user=isset($config['user'])?$config['user']:'root';
		$this->_pass=isset($config['pass'])?$config['pass']:'root';
		$this->_db=isset($config['db'])?$config['db']:'blogs';
	}
      private function _connect(){
      	$link = @mysql_connect($this->_host,$this->_user,$this->_pass);
      	if ($link) {
      		$this->_link = $link;
      	}else{
      		die('数据库连接失败，错误原因：'.mysql_error());
      	}
      }
      private function _charset(){
      	mysql_set_charset($this->_char);
      }
      private function _dbselect(){
      	mysql_select_db($this->_db);
      }
      public function query($sql){
       	$res=mysql_query($sql,$this->_link);
      	if($res){
      		$return = array();
      		while($row = mysql_fetch_assoc($res)){
      			$return[] = $row;
      		}
      		mysql_free_result($res);
      		return $return;
      	}else{
      		die('查询语句有误，语句为：'.$sql.',错误原因：'.mysql_error());
      	}
      }
private function exec(){
$res = mysql_qery($sql,$this->_link);
if($res){
return mysql_affected_rows();
}else{
die('增删语句有误，语句为：'.$sql.',错误原因：'.mysql_error());
}
}
public function __destruct(){
mysql_close($this->_link);
}


}
